#include <Wire.h>
#ifndef RX8010_ADDR
#define RX8010_ADDR 0x32
#endif
#define byte uint8_t
#define RX8010_SEC 0x10
#define RX8010_MIN 0x11
#define RX8010_HOUR 0x12
#define RX8010_WDAY 0x13
#define RX8010_MDAY 0x14
#define RX8010_MONTH 0x15
#define RX8010_YEAR 0x16
#define RX8010_RESV17 0x17
#define RX8010_ALMIN 0x18
#define RX8010_ALHOUR 0x19
#define RX8010_ALWDAY 0x1A
#define RX8010_TCOUNT0 0x1B
#define RX8010_TCOUNT1 0x1C
#define RX8010_EXT 0x1D
#define RX8010_FLAG 0x1E
#define RX8010_CTRL 0x1F
/* 0x20 to 0x2F are user registers */
#define RX8010_RESV30 0x30
#define RX8010_RESV31 0x31
#define RX8010_IRQ 0x32

/* Default values for reseting the module */
#define RX8010_ADDR17_DEF_VAL 0xD8
#define RX8010_ADDR30_DEF_VAL 0x00
#define RX8010_ADDR31_DEF_VAL 0x08
#define RX8010_ADDR31_DEF_VAL 0x08
#define RX8010_IRQ_DEF_VAL 0x04
#define RX8010_CTRL_DEF_VAL 0x04

#define RX8010_VLF_POS 1
#define RX8010_TE_POS 4
#define RX8010_TSEL0_POS 0
#define RX8010_TSEL1_POS 1
#define RX8010_TSEL2_POS 2
#define RX8010_TIE_POS 4
#define RX8010_TMPIN_POS 2
#define RX8010_TF_POS 4
#define RX8010_STOP_POS 6
#define RX8010_TSTP_POS 2
#define RX8010_AIE_POS 3
#define RX8010_WADA_POS 3
#define RX8010_AF_POS 3
#define RX8010_USEL_POS 5
#define RX8010_UF_POS 5
#define RX8010_UIE_POS 5
#define RX8010_FSEL0_POS 6
#define RX8010_FSEL1_POS 7
#define RX8010_FOPIN0_POS 0
#define RX8010_FOPIN1_POS 1

#define RX8010_AL_DISABLED 0b10000000
#define IRQ1 1
#define IRQ2 0
struct dateTime
{
    int year;
    int month;
    int date;
    int day;
    int hour;
    int minute;
    int second;
};

dateTime getDateTime();
void setTime(dateTime dt);
byte readFromModule(byte address);
byte setBinary(byte binary, byte pos, byte flagVal);
void writeToModule(byte address, byte data);
void writeFlag(byte address, byte pos, byte value);
byte getValueFromBinary(byte binary, byte pos);
uint8_t dec2bcd(uint8_t dec);
uint8_t bcd2dec(uint8_t bcd);
void resetRX8010();
// 13.2 WakeUp Timer Interrupt Function
void disableFCT();
// void enableFCT();
/******************************
 * setFCT(byte pin, uint16_t multiplier, byte frequency)
 * PIN IRQ1 / IRQ2
 * frequency=0 4096Hz   T:244.14us~15.9998s
 * frequency=1 64Hz     T:15.625ms~1023.984s
 * frequency=2 1Hz      T:1s~65535s
 * frequency=3 1/60Hz   T:1min~65535min
 * frequency=4 1/3600Hz T:1h~65535h
 * eg:setFCT(IRQ1, 2, 1);中断周期为2*1/64=0.03125s
 ******************************/
void setFCT(byte pin, uint16_t multiplier, byte frequency);

// Frequency output.(frequency/Hz)(1/1Hz)(2/1kHz)(3/32.8kHz);
/******************************
 * 13.6 FOUT function
 * PIN IRQ1 / IRQ2
 * frequency=1 1Hz
 * frequency=2 1kHz
 * frequency=3 32.8kHz Only IRQ1 Can output
 * ***************************/
void enableFOUT(byte pin, byte frequency);
void disableFOUT();
/******************************
 * TUI:TimeUpdateInterrutpFunction 13.4
 * output use IRQ1 only
 * minute=1 Output 1 interrupt per minute
 * minute=1 Output 1 interrupt per second
 * ***************************/
void setTUIMode(bool minute);
void enableTUI();
void disableTUI();
